<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 *
 */
namespace osc\admin\controller;
use osc\common\controller\AdminBase;
use think\Db;
class GroupBuy extends AdminBase{
	
	protected function _initialize(){
		parent::_initialize();
		$this->assign('breadcrumb1','扩展');
		$this->assign('breadcrumb2','团购');
	}
	
    public function index(){     	
		
		$list = Db::name('group_buy')->paginate(config('page_num'));
		$this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
		$this->assign('list', $list);
	
		return $this->fetch();

	 }
	 public	function add(){
	 	
		if(request()->isPost()){
			
			$data=input('param.');
			
			$groupbuy['group_image']=$data['group_image'];
			$groupbuy['group_name']=$data['group_name'];
			$groupbuy['goods_name']=$data['goods_name'];
			$groupbuy['group_price']=$data['group_price'];			
			$groupbuy['buy_limit']=$data['buy_limit'];
			
			$groupbuy['goods_price']=$data['price'];
			$groupbuy['quantity']=$data['quantity'];
			
			if(isset($data['goods_id'][0])){
				$groupbuy['goods_id']=$data['goods_id'][0];
			}
			
			$groupbuy['use_goods_des']=$data['use_goods_des'];
			$groupbuy['status']=$data['status'];
			$groupbuy['create_time']=time();
			$groupbuy['goods_price']=$data['price'];
			
			if(!empty($data['start_time'])){
				$groupbuy['start_time']=strtotime($data['start_time']);
			}
			if(!empty($data['end_time'])){
				$groupbuy['end_time']=strtotime($data['end_time']);
			}
			
			if($id=Db::name('group_buy')->insert($groupbuy,false,true)){
				
				if(isset($data['image'])){					
					foreach ($data['image'] as $k => $v) {
						Db::name('group_buy_images')->insert(array('group_buy_id'=>$id,'image'=>$v['image'],'alt'=>$v['description'],'sort_order'=>$v['sort_order']));
					}					
				}
				
				storage_user_action(UID,admin('username'),config('BACKEND_USER'),'新增了团购商品-'.$data['group_name']);
				
				$this->success('新增成功',url('GroupBuy/index'));	
			}else{
				$this->error('新增失败');	
			}
			
		}
		
		$this->assign('crumbs', '新增');
		$this->assign('action', url('GroupBuy/add'));
		return $this->fetch('edit');
	}
	 public	function edit(){
		
		if(request()->isPost()){
			
			$data=input('param.');
			$id=(int)$data['id'];
			unset($data['id']);
			$groupbuy['group_image']=$data['group_image'];
			$groupbuy['group_name']=$data['group_name'];
			$groupbuy['goods_name']=$data['goods_name'];
			$groupbuy['group_price']=$data['group_price'];
			$groupbuy['buy_limit']=$data['buy_limit'];
			
			$groupbuy['goods_price']=$data['price'];
			$groupbuy['quantity']=$data['quantity'];
			
			if(isset($data['goods_id'][0])){
				$groupbuy['goods_id']=$data['goods_id'][0];
			}
			$groupbuy['use_goods_des']=$data['use_goods_des'];
			$groupbuy['status']=$data['status'];
		
			$groupbuy['goods_price']=$data['price'];
			
			if(!empty($data['start_time'])){
				$groupbuy['start_time']=strtotime($data['start_time']);
			}
			if(!empty($data['end_time'])){
				$groupbuy['end_time']=strtotime($data['end_time']);
			}
			
			if(Db::name('group_buy')->where('id',$id)->update($groupbuy)){
				
				if(isset($data['image'])){
					
					Db::name('group_buy_images')->where('group_buy_id',$id)->delete();
								
					foreach ($data['image'] as $k => $v) {
						Db::name('group_buy_images')->insert(array('group_buy_id'=>$id,'image'=>$v['image'],'alt'=>$v['description'],'sort_order'=>$v['sort_order']));
					}					
				}
				
				storage_user_action(UID,admin('username'),config('BACKEND_USER'),'修改了团购商品-'.$data['group_name']);
				
				$this->success('修改成功',url('GroupBuy/index'));	
			}else{
				$this->error('修改失败');	
			}
			
		}

		$this->assign('image',Db::name('group_buy_images')->where('group_buy_id',(int)input('param.id'))->select());
		$this->assign('d',Db::name('GroupBuy')->find((int)input('param.id')));
		$this->assign('crumbs', '修改');
		$this->assign('action', url('GroupBuy/edit'));	
			
		return $this->fetch('edit');
	}
	public	function del(){
		
		$id=(int)input('param.id');
		
		Db::name('group_buy')->where('id',$id)->delete();
		Db::name('group_buy_images')->where('group_buy_id',$id)->delete();
		storage_user_action(UID,admin('username'),config('BACKEND_USER'),'删除了团购商品-'.$id);
		
		$this->redirect('GroupBuy/index');
	}
	public function set_status(){
		$data=input('param.');
		
		Db::name('group_buy')->where('id',$data['id'])->update(['status'=>(int)$data['status']],false,true);		
		
		storage_user_action(UID,admin('username'),config('BACKEND_USER'),'修改了团购商品状态');	
		
		$this->redirect('GroupBuy/index');
	}
}
?>